package com.wt.struggle.goodbye.main.activity

import android.app.Activity
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.Gravity
import android.view.Window
import android.view.WindowManager
import android.widget.FrameLayout
import android.widget.LinearLayout
import com.wt.struggle.goodbye.R
import com.wt.struggle.goodbye.base.Config
import com.wt.struggle.goodbye.base.Send
import com.wt.struggle.goodbye.info.NewsInfo
import com.wt.struggle.goodbye.info.ProUserInfo
import com.xin.lv.yang.utils.utils.DataUtil
import com.xin.lv.yang.utils.view.CustomToast
import kotlinx.android.synthetic.main.add_comment_pop.*
import org.json.JSONObject

/**
 * 添加评价
 */
class AddCommentActivity : AppCompatActivity() {

    /**
     *  1 为内容评论   2 为评论信息评论  3 私信回复
     */
    var joupCode = 1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val send = Send(this)

        setFinishOnTouchOutside(true)
        requestWindowFeature(Window.FEATURE_NO_TITLE)

        val lp: WindowManager.LayoutParams = window.attributes
        //设置窗口宽度
        lp.width = getW()
        //设置窗口高度
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT
        // 设置Dialog位置
        lp.gravity = Gravity.BOTTOM

        Log.i("result", "lp-------" + lp.width)

        window.attributes = lp

        setContentView(R.layout.add_comment_pop)

        addCommentLinear.layoutParams = FrameLayout.LayoutParams(getW(), FrameLayout.LayoutParams.WRAP_CONTENT)

        val infoId = intent.getIntExtra("id", 0)

        joupCode = intent.getIntExtra("code", 1)

        buttonSend.setOnClickListener {

            val title = etAddComment.text.toString()

            if (title != "") {
                val userInfo = ProUserInfo()
                when (joupCode) {
                    1 -> {
                        userInfo.contentId = infoId
                        userInfo.content = DataUtil.getString(title)
                        send.addComment(userInfo, handler)
                    }
                    2 -> {
                        userInfo.content = DataUtil.getString(title)
                        userInfo.commentId = infoId.toString()
                        send.addComment(userInfo, handler)
                    }
                    3 -> {
                        val toId = intent.getIntExtra("toId", 0)
                        send.sendSiXinHuiFu(toId, infoId, DataUtil.getString(title), handler)
                    }
                }

            } else {
                showToastShort("请写点什么吧！！")
            }

        }

    }


    fun showToastShort(s: String) {
        CustomToast.showToast(this, Gravity.CENTER, 0, s)

    }

    val handler = object : Handler(Looper.getMainLooper()) {
        override fun handleMessage(msg: Message?) {
            super.handleMessage(msg)
            when (msg!!.what) {

                Send.ADD_COMMENT -> {

                    val json = JSONObject(msg.obj as String)
                    val jsonObject = json.optJSONObject("head")

                    if (jsonObject != null) {

                        val resultCode = jsonObject.optInt("response_status")
                        val resultMessage = jsonObject.optString("response_msg")

                        if (resultCode == Config.SUCCESS) {

                            if (joupCode == 1) {
                                showToastShort("评论成功")

                            } else if (joupCode == 2) {

                                showToastShort("回复成功")
                            }

                            setResult(Activity.RESULT_OK)
                            finish()
                        }
                    }
                }

                Send.DELETE_RECYCLE_CODE -> {
                    val json = JSONObject(msg.obj as String)
                    val jsonObject = json.optJSONObject("head")

                    if (jsonObject != null) {

                        val resultCode = jsonObject.optInt("response_status")
                        if (resultCode == Config.SUCCESS) {

                            showToastShort("私信回复成功")
                            setResult(Activity.RESULT_OK)
                            finish()
                        }
                    }

                }
            }

        }
    }


    fun getW(): Int {
        val att = windowManager.defaultDisplay;
        return att.width
    }

    fun getH(): Int {
        val att = windowManager.defaultDisplay;
        return att.height
    }

}